package com.frezrik.jetpackpractice.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModelProvider;

public class DataViewModel extends ViewModel {
    private final static String SAVE_KEY = "save_key";
    private SavedStateHandle mSavedStateHandle;

    public DataViewModel(SavedStateHandle savedStateHandle) {
        if (!savedStateHandle.contains(SAVE_KEY)) {
            savedStateHandle.set(SAVE_KEY, 0);
        }
        mSavedStateHandle = savedStateHandle;
    }

    public MutableLiveData<Integer> getData() {
        return mSavedStateHandle.getLiveData(SAVE_KEY);
    }

    public void add() {
        mSavedStateHandle.set(SAVE_KEY, (int) mSavedStateHandle.get(SAVE_KEY) + 1);
    }
}
